/**
 * 
 */
package com.aodesoft.demo.dwr;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.annotation.Resource;

import org.directwebremoting.annotations.RemoteProxy;
import org.directwebremoting.annotations.ScriptScope;
import org.directwebremoting.spring.SpringCreator;

import com.aodesoft.demo.dao.DemoDao;
import com.aodesoft.pojo.Demo;
import com.aodesoft.pojo.DemoReplay;
import com.googlecode.genericdao.search.Search;

/**
 * 
 * @author tang
 * @date 2011-12-6
 */
@RemoteProxy(creator=SpringCreator.class,scope=ScriptScope.APPLICATION)
public class DemoDwr {
	@Resource
	DemoDao demoDao;
	public String test(){
		return "<div style=\"color:#"+Integer.toHexString(new Random().nextInt()).substring(2)+"\">我是一只草泥马~</div>";
	}
	public DemoReplay query(){
		DemoReplay ds=new DemoReplay();
		ds.setId(1);
		ds.setContent("内容");
		ds.setDate(new Timestamp(new Date().getTime() ));
		return ds;
	}
	/**
	 * 分页查询
	 * @author tang
	 * @date 2011-12-6
	 * @param pageSize 每页显示数目
	 * @param pageIndex 第几页
	 * @return
	 */
	public List<Demo> getAll(int pageSize,int pageIndex ){
		Search s=new Search();
		//按ID降序
		s.addSortDesc("id");
		//每页显示数目
		s.setMaxResults(pageSize);
		//第几页
		s.setPage(pageIndex);
		//执行查询
		List<Demo> lst= demoDao.search(s);
		return lst;
	}
	/**
	 * 总数
	 * @author tang
	 * @date 2011-12-6
	 * @return
	 */
	public int count(){
		return demoDao.count(new Search());
	}
	/**
	 * 删除
	 * @author tang
	 * @date 2011-12-7
	 * @param id
	 * @return
	 */
	public Boolean delDemo(int  id){
		return demoDao.removeById(id);
	}
	/**
	 * 批量删除
	 * @author tang
	 * @date 2011-12-12
	 * @param id
	 */
	//这里要用Integer而不是int，用int会报错
	public void delDemos(Integer[] id ){
		try {
			demoDao.removeByIds(id); 
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
